﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorldView;
using System.Windows.Forms;
using System.Threading;

namespace TerrariaWorldViewerCmd
{
    public class CommandLineWorldMapper
    {
        System.Threading.Timer timer;
        ProgressBar progressBar = new ProgressBar();
        int? lastProgress = null;

        public CommandLineWorldMapper()
        {
            ResourceManager.Instance.Initialize();
            SettingsManager.Instance.Initialize();
        }

        public void GenerateWorld(Options options)
        {            
            SettingsManager.Instance.IsSymbolsDrawable = options.UseSymbols;
            SettingsManager.Instance.IsWallDrawable = options.DrawWalls;

            timer = new System.Threading.Timer((TimerCallback)Timer_Tick, null, 100, 100);
            var mapper = new WorldMapper();
            mapper.Initialize();

            //try
            {
                mapper.OpenWorld(options.SourceWorld);
            }
            //catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //   return;
            }

            Console.WriteLine("");
            Console.WriteLine("Generating World PNG...");
            mapper.CreatePreviewPNG(options.TargetPng, options.DrawWalls, options.UseSymbols, progressBar);
            DrawTextProgressBar(progressBar.Maximum, progressBar.Maximum);
            mapper.CloseWorld();

            Console.WriteLine("");
            SettingsManager.Instance.Shutdown();
        }

        protected void Timer_Tick(Object stateInfo)
        {
            DrawTextProgressBar(progressBar.Value, progressBar.Maximum);
        }

        protected void DrawTextProgressBar(int progress, int total)
        {
            if (!lastProgress.HasValue || progress != lastProgress)
            {
                //draw empty progress bar
                Console.CursorLeft = 0;
                Console.Write("["); //start
                Console.CursorLeft = 32;
                Console.Write("]"); //end
                Console.CursorLeft = 1;
                float onechunk = 30.0f / total;

                //draw filled part
                int position = 1;
                for (int i = 0; i < onechunk * progress; i++)
                {
                    Console.BackgroundColor = ConsoleColor.Gray;
                    Console.CursorLeft = position++;
                    Console.Write(" ");
                }

                //draw unfilled part
                for (int i = position; i <= 31; i++)
                {
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.CursorLeft = position++;
                    Console.Write(" ");
                }

                //draw totals
                Console.CursorLeft = 35;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Write(progress.ToString() + " of " + total.ToString() + "    "); //blanks at the end remove any excess
            }
            lastProgress = progress;
        }
    }
}
